<?php

namespace app\bbs\controller;

use think\Controller;
use think\Db;
use think\Session;

class Loginandreg extends Controller
{
    public function index()
    {
        $go_href = input('get.url', url('bbs/classification/index'));
        $image = createImage();
        $bbs_settings = Db::table('bbs_settings')
            ->where('id', 'eq', 1)
            ->find();
        $bbs_settings['rsa_public'] = str_replace("\n", '', $bbs_settings['rsa_public']);
        $bbs_settings['rsa_private'] = str_replace("\n", '', $bbs_settings['rsa_private']);
        return $this->fetch('loginandreg', ['image' => $image, 'bbs_settings' => $bbs_settings, 'go_href' => $go_href]);
    }
    public function GetQrcode()
    {
        $this->success('success', null, ['base64_img' => createImage()]);
    }
    public function login()
    {
        $username = rsa_decode(input('post.username', null));
        $password = rsa_decode(input('post.password', null));
        $verifycode = input('post.verifycode', null);
        if ($username && $password && $verifycode && $verifycode == session('loginandreg_qrcode')) {
            #判断 是不是用户名 / 邮箱 +密码组合
            $user_info = Db::table('user')
                ->whereOr([
                    'email' => $username,
                    'user_name' => $username,
                ])
                ->where(['password' => $password])
                ->select();
            if ($user_info && count($user_info) == 1) {
                Session::set('bbs_user_info', $user_info[0]);
                $Data['code'] = '0';
                $Data['ErrorPosition'] = 'NoError';
                $Data['ErrorMsg'] = 'null';
                $Data['tips'] = '';
                return json($Data);
            } else {
                if (count($user_info) != 1 && $user_info != null) {
                    $Data['code'] = '10004';
                    $Data['ErrorPosition'] = 'LoginAndReg-Login';
                    $Data['ErrorMsg'] = '根据提供的用户名/邮箱发现多个用户请尝试更换邮箱登陆';
                    $Data['tips'] = 'login-username';
                    return json($Data);
                } else if (!$user_info) {
                    $Data['code'] = '10005';
                    $Data['ErrorPosition'] = 'LoginAndReg-Login';
                    $Data['ErrorMsg'] = '用户名或密码错误';
                    $Data['tips'] = 'login-username';
                    return json($Data);
                }
            }
        } else {
            if (!$username) {
                $Data['code'] = '10000';
                $Data['ErrorPosition'] = 'LoginAndReg-Login';
                $Data['ErrorMsg'] = '没有填写邮箱地址或用户名';
                $Data['tips'] = 'login-username';
                return json($Data);
            } else if (!$password) {
                $Data['code'] = '10001';
                $Data['ErrorPosition'] = 'LoginAndReg-Login';
                $Data['ErrorMsg'] = '没有填写密码';
                $Data['tips'] = 'login-password';
                return json($Data);
            } else if (!$verifycode) {
                $Data['code'] = '10002';
                $Data['ErrorPosition'] = 'LoginAndReg-Login';
                $Data['ErrorMsg'] = '没有填写验证码';
                $Data['tips'] = 'login-verify-code';
                return json($Data);
            } else if ($verifycode != session('loginandreg_qrcode')) {
                $Data['code'] = '10003';
                $Data['ErrorPosition'] = 'LoginAndReg-Login';
                $Data['ErrorMsg'] = '验证码不匹配';
                $Data['tips'] = 'login-verify-code';
                return json($Data);
            }
        }
    }
    public function SonVerificationCode()
    {
        $email = rsa_decode(input('email', null));
        $input_id = rsa_decode(input('input_id', null));

        if ($email && $input_id) {
            switch ($input_id) {
                case 'Register_Email': //注册
                    $code = createCode();
                    $time = time();
                    $start = Db::table('bbs_user_verification_code')
                        ->where([
                            'mail' => $email,
                            'start' => 0
                        ])
                        ->where("out_time > '$time'")
                        ->find();
                    if ($start) {
                        $Data['code'] = '10001';
                        $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                        $Data['ErrorMsg'] = '验证码未过期';
                        return json($Data);
                    } else {

                        $html = VerificationCodeString($code);
                        $return = D_Mail('欢迎注册 CloverDownload', $email, $html, '欢迎注册 CloverDownload');
                        if ($return) {
                            Db::table('bbs_user_verification_code')
                                ->insert([
                                    'mail' => $email,
                                    'code' => $code,
                                    'add_time' => time(),
                                    'start' => 0,
                                    'out_time' => time() + 900,
                                ]);
                            $Data['code'] = '0';
                            $Data['ErrorPosition'] = 'NoError';
                            $Data['ErrorMsg'] = '';
                            return json($Data);
                        } else {
                            $Data['code'] = '10002';
                            $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                            $Data['ErrorMsg'] = '邮箱地址错误 或 发件系统错误 请检查';
                            return json($Data);
                        }
                    }
                    break;
                case 'Retrieve_Email': //注册
                    $code = createCode();
                    $time = time();
                    $start = Db::table('bbs_user_verification_code')
                        ->where([
                            'mail' => $email,
                            'start' => 0
                        ])
                        ->where("out_time > '$time'")
                        ->find();
                    if ($start) {
                        $Data['code'] = '10001';
                        $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                        $Data['ErrorMsg'] = '验证码未过期';
                        return json($Data);
                    } else {

                        $html = VerificationCodeString($code);
                        $return = D_Mail('欢迎注册 CloverDownload', $email, $html, '欢迎注册 CloverDownload');
                        if ($return) {
                            Db::table('bbs_user_verification_code')
                                ->insert([
                                    'mail' => $email,
                                    'code' => $code,
                                    'add_time' => time(),
                                    'start' => 0,
                                    'out_time' => time() + 900,
                                ]);
                            $Data['code'] = '0';
                            $Data['ErrorPosition'] = 'NoError';
                            $Data['ErrorMsg'] = '';
                            return json($Data);
                        } else {
                            $Data['code'] = '10002';
                            $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                            $Data['ErrorMsg'] = '邮箱地址错误 或 发件系统错误 请检查';
                            return json($Data);
                        }
                    }
                    break;

                default:
                    # code...
                    break;
            }
        } else {
            $Data['code'] = '10000';
            $Data['ErrorPosition'] = 'LoginAndReg-Reg';
            $Data['ErrorMsg'] = '没有填写邮件地址';
            return json($Data);
        }
    }
    public function register()
    {
        $time = time();
        $email = rsa_decode(input('email', null));
        $RegisterCode = rsa_decode(input('RegisterCode', null));
        $password = rsa_decode(input('password', null));
        $repassword = rsa_decode(input('repassword', null));
        $qrcode = input('qrcode', null);
        if ($email  && $password  && $repassword  && $qrcode  && $RegisterCode  && $password == $repassword && preg_match('/(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[A-Za-z0-9]{6,32}/', $password)) {
            $repeat = Db::table('user') //判断又没有重复的邮箱地址
                ->where(['email' => $email])
                ->find();
            if ($repeat) {
                $Data['code'] = '10011';
                $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                $Data['ErrorMsg'] = '邮箱或用户名已存在';
                $Data['tips'] = 'Register_Email';
                return json($Data);
            }
            if (session('loginandreg_qrcode') == $qrcode) {
                $start = Db::table('bbs_user_verification_code')
                    ->where([
                        'mail' => $email,
                        'code' => $RegisterCode,
                        'start' => 0
                    ])
                    ->where("out_time > '$time'")
                    ->find();
                if ($start) {
                    $reg_user_id =  Db::table('user')
                        ->insertGetId([
                            'user_name' => $email,
                            'password' => md5($password),
                            'email' => $email,
                            'head_src' => 'https://wx1.sinaimg.cn/large/006wvbvUly1g3dn2lgc4ij32yo1o0npf.jpg',
                            'user_jurisdiction' => '10',
                            'number_of_violations' => '0',
                        ]);
                    Db::table('bbs_user_integral')
                        ->insert([
                            'user_id' => $reg_user_id,
                            'download_currency' => 0,
                            'integral' => 0,
                        ]);
                    Db::table('bbs_user_verification_code')
                        ->where([
                            'mail' => $email,
                            'code' => $RegisterCode,
                            'start' => 0
                        ])
                        ->update([
                            'start' => 1
                        ]);
                    session('loginandreg_qrcode', null);
                    $Data['code'] = '0';
                    $Data['ErrorPosition'] = 'NoError';
                    $Data['ErrorMsg'] = '';
                    return json($Data);
                } else {
                    $Data['code'] = '10010';
                    $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                    $Data['ErrorMsg'] = '邮箱或验证码 错误 或 验证码过期';
                    $Data['tips'] = 'register-code';
                    return json($Data);
                }
            } else {
                $Data['code'] = '10009';
                $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                $Data['ErrorMsg'] = '验证码不匹配';
                $Data['tips'] = 'qrcode3_input';
                return json($Data);
            }
        } else {
            if (!$email) {
                $Data['code'] = '10003';
                $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                $Data['ErrorMsg'] = '没有填写邮箱地址';
                $Data['tips'] = 'Register_Email';
                return json($Data);
            }
            if (!$RegisterCode) {

                $start = Db::table('bbs_user_verification_code')
                    ->where([
                        'mail' => $email,
                        'start' => 0
                    ])
                    ->where("out_time > '$time'")
                    ->find();
                if (!$start) {
                    $Data['code'] = '10003';
                    $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                    $Data['ErrorMsg'] = '请获取验证码';
                    $Data['tips'] = 'Register_Email';
                    return json($Data);
                } else {
                    $Data['code'] = '10004';
                    $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                    $Data['ErrorMsg'] = '请输入邮件验证码';
                    $Data['tips'] = 'register-code';
                    return json($Data);
                }
            }
            if (!$password) {
                $Data['code'] = '10005';
                $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                $Data['ErrorMsg'] = '没有填写密码';
                $Data['tips'] = 'register-password';
                return json($Data);
            }
            if (!preg_match('/(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[A-Za-z0-9]{6,32}/', $password)) {
                $Data['code'] = '10006';
                $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                $Data['ErrorMsg'] = '密码应该包含大小写和数字 并且 6-32位';
                $Data['tips'] = 'register-password';
                return json($Data);
            }
            if (!$repassword) {
                $Data['code'] = '10007';
                $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                $Data['ErrorMsg'] = '请确认密码';
                $Data['tips'] = 'register-repassword';
                return json($Data);
            }
            if ($password != $repassword) {
                $Data['code'] = '10008';
                $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                $Data['ErrorMsg'] = '两次密码不一致';
                $Data['tips'] = 'register-repassword';
                return json($Data);
            }
            if (!$qrcode) {
                $Data['code'] = '10009';
                $Data['ErrorPosition'] = 'LoginAndReg-Reg';
                $Data['ErrorMsg'] = '请输入验证码';
                $Data['tips'] = 'qrcode3_input';
                return json($Data);
            }
        }
    }
    public function login_out()
    {
        session(null);
    }
}
